package com.nopay.cdxphone.base;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.azhon.basic.eventbus.MessageEvent;
import com.azhon.basic.utils.ActivityManagerUtils;
import com.nopay.cdxphone.utils.ClassUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import com.nopay.cdxphone.net.AssetsRetrofit;
import com.nopay.cdxphone.net.HttpApi;

import retrofit2.Retrofit;

public abstract class BaseNoSwipeActivity<T extends ViewBinding> extends AppCompatActivity {

    private final int mRequestCode = 1024;
    protected boolean isDestroyed = false;
    protected AppCompatActivity mCurrentActivity;
    private View rootView;
    protected T binding;
    private Retrofit retrofit = AssetsRetrofit.getInstance().getRetrofit();
    protected HttpApi api = retrofit.create(HttpApi.class);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 隐藏标题栏
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        fullScreen(this);

        ActivityManagerUtils.getInstance().addActivity(this);
        binding = getBinding();
        rootView = binding.getRoot();
        setContentView(binding.getRoot());

        init(savedInstanceState);
        EventBus.getDefault().register(this);
        mCurrentActivity = this;
    }

    /**
     * 抽象 - 初始化方法，可以对数据进行初始化
     */
    protected abstract void init(Bundle savedInstanceState, Object... preData);

    @Override
    protected void onResume() {
        super.onResume();
        Resources resources = this.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.fontScale = 1;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }

    /**
     * 通过设置全屏，设置状态栏透明 * * @param activity
     */
    private void fullScreen(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                //5.x开始需要把颜色设置透明，否则导航栏会呈现系统默认的浅灰色
                Window window = activity.getWindow();
                View decorView = window.getDecorView();
                //两个 flag 要结合使用，表示让应用的主体内容占用系统状态栏的空间
                int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
                decorView.setSystemUiVisibility(option);
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(Color.TRANSPARENT);
            } else {
                Window window = activity.getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
                int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
                attributes.flags |= flagTranslucentStatus;
                window.setAttributes(attributes);
            }
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            destroy();
        }
    }

    @Override
    protected void onDestroy() {
        destroy();
        super.onDestroy();
    }

    @Override
    public boolean isDestroyed() {
        return isDestroyed;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDataEvent(MessageEvent msg) {
        switch (msg.msgType) {
        }
    }

    @Override
    public void onBackPressed() {
    }


    private void destroy() {
        if (isDestroyed) {
            return;
        }
        EventBus.getDefault().unregister(this);
        ActivityManagerUtils.getInstance().removeActivity(this);
        isDestroyed = true;
    }

    protected T getBinding() {
        try {
            Type superClass = getClass().getGenericSuperclass();
            Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
            Class<?> clazz = ClassUtils.getRawType(type);
            Method method = clazz.getMethod("inflate", LayoutInflater.class);
            return (T) method.invoke(null, getLayoutInflater());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
